package mvp.com.mvpdemo.presenter;

import android.os.Handler;

import mvp.com.mvpdemo.model.IUserBiz;
import mvp.com.mvpdemo.model.UserBiz;
import mvp.com.mvpdemo.model.bean.User;
import mvp.com.mvpdemo.view.IUserLoginView;

/**
 * Created by YoungerChen on 2016/9/13.
 */
public class UserLoginPresenter {

    private IUserBiz userBiz;
    private IUserLoginView loginView;
    private Handler mHandler = new Handler();

    public UserLoginPresenter (IUserLoginView loginView) {

        this.loginView = loginView;
        userBiz = new UserBiz();

    }

    public void login() {
        // 显示进度条
        loginView.showLoading();
        // 调用model层的登陆方法
        userBiz.login(loginView.getUsername(), loginView.getPassword(), new IUserLoginView.OnLoginListener() {

            @Override
            public void loginSuccess(User user) {
                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        loginView.toMainActivity();
                        loginView.hideLoading();
                    }
                });
            }

            @Override
            public void loginFailed() {
                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        loginView.showFailedError();
                        loginView.hideLoading();
                    }
                });
            }

        });
    }

    public void clear() {
        loginView.clearPassword();
        loginView.clearUsername();
    }

}
